/**
 * 主界面
 */
class MainUI extends UIBase{
    private btn_sign:BaseButton;
    private group_new:eui.Group;
    private btn_customs:BaseButton;
    private btn_sound:BaseButton;
    private btn_achievement:BaseButton;

    private img_guide:eui.Image;
    private btn_award:eui.Group;
    private btn_award_1:BaseButton;
    private btn_gift:BaseButton;
    private img_circle:eui.Image;

    public btn_getGold:BaseButton;
    private activity_star:egret.MovieClip;
    private system:particle.ParticleSystem;
    private img_tittle:eui.Image;
    private btn_all:BaseButton;

    private btn_friendRank:BaseButton;
    private btn_worldRank:BaseButton;
    // private aim_box:AimBoxUI;
    private img_bg:eui.Image;
    private img_spt9:eui.Image;
    private bottom_btn_group:eui.Group;

    private resourceItem:ResourceItem;
    private Timer:egret.Timer;

    constructor()
    {
        super('MainView');
        let platform = egret.Capabilities.os;
        if(platform != 'Windows PC')
        SoundManager.instance().playCustomsSound();
        // TTSoundManager.instance().playSound('resource/assets/music/level.mp3');
    }

    public init()
    {
        super.init(true);
        //预加载音乐看看效果
        this.initAnimation();
        
        this.addParticle();
        // this.group_touch.visible = false;
        this.openUIHandle();
        this.btn_sound.currentState = SoundManager.instance().isBGMPlay ? 'enabled':'disabled';
        this.reviewHide();
        GameManager.instance.init();
        // this.setDate();
        if(RoleCache.instance().isGameFirstLogin)
        {
            this.initRedPoint();
            RoleCache.instance().isGameFirstLogin = false;
        }
        else
        {
            GameUtil.requsetRedPoint(['achieve'], this.initRedPoint, this);
        }
        this.Timer = new egret.Timer(3000);//红点计时器
        this.Timer.start();
    }

    protected fixIphoneX()
    {
        super.fixIphoneX();
        if(GlobalManager.instance().isIphonex())
        {
            this.bottom_btn_group.bottom = 297;
        }
    } 

    public setDate()
    {
        if(TimeUtil.getIsNight())
        {
            this.img_bg.source = 'bg2_png';
            this.img_spt9.source = 'spt9_bg_di_2_png';
        }
        else
        {
            this.img_bg.source = 'bg_jpg';
            this.img_spt9.source = 'spt9_bg_di_png';
        }
    }

    protected reviewHide()
    {
        if(RoleCache.instance().reviewStatus)
        {
            this.btn_gift.visible = false;
            this.img_circle.visible = false;
            // this.box.visible = false;
        }
        else
        {
            UIManager.instance().showLamp();
            // this.aim_box.visible = true;
        }
    }

    /**
     * 处理上线弹窗逻辑
     * 优先级
     * 获奖 离线收益 签到
     */
    protected openUIHandle()
    {
        let platform = egret.Capabilities.os;
        let is_week_login = RoleCache.instance().isFirstWeekEnter; //是否是本周第一次登陆
        if(is_week_login)
        {
            RankManager.instance().uploadUserScore(0, 'points');
        }
        if(!RoleCache.instance().reviewStatus && platform != 'Windows PC')
        {
            let avatar_url = RoleCache.instance().avatar_url;
            let is_win_award = RoleCache.instance().isWeekLucky;//是否中奖了
            
            let is_first_login = RoleCache.instance().isFirstEnter; //是否今天第一次登陆
            let is_sign = RoleCache.instance().isSign; //今天是否签到过了
            let offline_money = RoleCache.instance().offline_money;
            let aimUI;//需要打开的目标界面
            let data = {
                isPop : true,
                effect : 1
            }

            if(avatar_url == '')
            {
                //未授权
                aimUI = WechatConfirmUI;
                // WeChatgGameManager.instance().getUserInfo((info)=>{});
            }
            else if(is_week_login && is_win_award)
            {
                aimUI = GiftUI;
            //     else
            //     {
            //         //别人中奖了
            //         aimUI = EndLessWinListUI;
            //     }
            }
            else if(offline_money > 0)
            {
                aimUI = OfflineUI;
            }
            else if(!is_sign && is_first_login)
            {
                aimUI = SignUI;
            }

            if(aimUI != null)
            {
                egret.setTimeout(()=>{
                    UIManager.instance().openUILogic(aimUI, data);
                },this, 300);
            }
            
            RoleCache.instance().isFirstEnter = false;
        }
    }

    public addParticle()
    {
        let texture = RES.getRes('leaf_png');
        let config = RES.getRes("leaf_json");
        this.system = new particle.GravityParticleSystem(texture, config);
        this.addChildAt(this.system,1);
        this.system.emitterX = 0;
        this.system.emitterY = 0;
        this.system.start();
    }

    public addGif()
    {
        if(!this.activity_star)
        {
            this.activity_star = MovieManager.instance().getGifByName('activityAni', 'activity_ani');
            this.btn_award.addChild(this.activity_star);
            this.activity_star.anchorOffsetX = this.activity_star.width >> 1;
            this.activity_star.anchorOffsetY = this.activity_star.height >> 1;
            this.activity_star.x = 0;
            this.activity_star.y = -20;
            this.activity_star.play(-1);
        }
    }

    //定时器走的函数
    public timerHandle()
    {
        this.checkFoodActivityRedPoint();
    }

    protected addEvent()
    {
        this.btn_achievement.addTapHandler(this.showAchievement, this);
        this.btn_customs.addTapHandler(this.startGame, this);
        // this.group_touch.addEventListener(egret.TouchEvent.TOUCH_TAP, this.hideBox, this);
        this.btn_sound.addTapHandler(this.soundHandle, this);
        // this.btn_rank.addTapHandler(this.rankHandle, this);
        this.group_new.addEventListener(egret.TouchEvent.TOUCH_TAP, this.newWeponHandle, this);
        // this.btn_box.addTapHandler(this.jumpToOther,this);
        this.btn_sign.addTapHandler(this.signHandle, this);

        this.img_guide.addEventListener(egret.TouchEvent.TOUCH_TAP, this.showGuide, this);
        this.btn_gift.addTapHandler(this.gfitShow, this);
        this.btn_award.addEventListener(egret.TouchEvent.TOUCH_TAP,this.AwardHadnle,this);

        this.btn_getGold.addTapHandler(this.addGold, this);
        this.btn_all.addTapHandler(this.startEndlessGame, this);
        this.btn_friendRank.addTapHandler(this.showFriendRank, this);
        this.btn_worldRank.addTapHandler(this.showWorldRank, this);
        GameUtil.addEventListener(EventConst.UPDATE_REDPOINT, this.updateRedPointHandler, this);
        this.Timer.addEventListener(egret.TimerEvent.TIMER, this.timerHandle, this);
        // this.more_group.addEventListener(egret.TouchEvent.TOUCH_TAP, this.moreGroupShow, this);
    }

    /**
     * 成就界面
     */
    public showAchievement()
    {
        let data = {
            isPop:true,
            effect:1,
            showBG:true
        }
        UIManager.instance().openUILogic(AchievementUI, data);
    }

    //初始化红点状态
    private initRedPoint()
    {
        let arr = [RedPointID.Sign, RedPointID.invite, RedPointID.Achievement,RedPointID.Activity];
        for(let v of arr)
        {
            this.updateRedPoint(v);
        }
    }

    //接收红点更新事件
    protected updateRedPointHandler(e:BaseEvent){
        let data = e['param'];
        this.updateRedPoint(data['redid']);
    }

    //更新红点状态
    public updateRedPoint(type:number)
    {
        let state = false;
        switch(type){
			case RedPointID.Sign:
                state = !RoleCache.instance().isSign;
				this.btn_sign.redPoint = state;
                break;
            case RedPointID.invite:
                state = !RoleCache.instance().inviteRedPoint;
				this.btn_getGold.redPoint = state;
            case RedPointID.Activity:
                this.checkFoodActivityRedPoint();
            case RedPointID.Achievement:
                state = RoleCache.instance().isHaveAchievement;
                this.btn_achievement.redPoint = state;
            default:
                break;
		}
    }

    //检测晚午餐红点
    public checkFoodActivityRedPoint()
    {
        
        if(TimeUtil.judgeIsInTimeRange('12:00','14:00') && !RoleCache.instance().isGetLunchAward)
        {
            //午餐
            this.btn_award_1.redPoint = true;
            return;
        }
        if(TimeUtil.judgeIsInTimeRange('16:00','21:00') && !RoleCache.instance().isGetDinnerAward)
        {
            //晚餐
            this.btn_award_1.redPoint = true;
            return;
        }
        this.btn_award_1.redPoint = false;
    }

    protected showFriendRank()
    {
        let data = {
            isPop:true,
            effect:1
        }
        UIManager.instance().openUILogic(FriendRank, data);
    }

    protected showWorldRank()
    {
        let avatar_url = RoleCache.instance().avatar_url;
        if(avatar_url != '')
        {
            let data = {
                isPop:true,
                effect:1
            }
            UIManager.instance().openUILogic(WorldRankUI, data);
        }
        else
        {
            //授权
            let data = {
                isPop:true,
                effect:1,
                showBG:true,
                type:confirmType.openWorldRank
            }
            UIManager.instance().openUILogic(WechatConfirmUI, data);
        }
    }

    protected hideBox()
    {
        // this.box.show();
        // this.group_touch.visible = false;
    }

    /**
     * 开启无尽模式
     */
    public startEndlessGame()
    {
        let avatar_url = RoleCache.instance().avatar_url;
        if(avatar_url != '')
        {
            //授权后允许进入
            let data = {
                isPop:true,
                effect:1
            }
            UIManager.instance().openUILogic(EndLessSamllRankUI,data);
        }
        else
        {
            //授权
            let data = {
                isPop:true,
                effect:1,
                showBG:true,
                type:confirmType.enterEndLessGame
            }
            UIManager.instance().openUILogic(WechatConfirmUI, data);
        }
    }

    /**
     * 改为邀请有礼
     */
    protected addGold()
    {
        let data = {
            isPop:true,
            effect:3
        }
        UIManager.instance().openUILogic(ActivityInviteUI, data);
    }

    /**
     * 福利中心
     */
    protected AwardHadnle()
    {
        EffectUtil.instance().playBtnEffect(this.btn_award);
        egret.Tween.get(this.btn_award).wait(200).call(()=>{
			let data = {
                isPop:true,
                effect:3
            }
            UIManager.instance().openUILogic(ActivityUI, data);
		});
    }

    /**
     * 送礼界面
     */
    protected gfitShow()
    {
        let data = {
            isPop:true,
            effect:1
        }
        UIManager.instance().openUILogic(GiftUI,data);
    }

    //改为显示领金币
    protected showGuide()
    {
        // let data = {
        //     isPop:true,
        //     effect:4
        // }
        // UIManager.instance().openUILogic(StateUI,data);
        // let data = {
        //     isPop:true,
        //     effect:3
        // }
        // UIManager.instance().openUILogic(AddGoldUI, data);
        MessageUtil.tips('啥子也没有你说气不气!');
    }

    /**
     * 签到
     */
    protected signHandle()
    {
        let data={
            isPop:true,
            effect:1
        }
        UIManager.instance().openUILogic(SignUI,data);
    }

    /**
     * 武器
     */
    private newWeponHandle()
    {
        let data = {
            isPop : true,
            effect:0
        }
        UIManager.instance().openUILogic(WeaponNewSelectUI, data);
    }

    //初始化动画写在这里
	protected initAnimation()
	{
		// EffectUtil.instance().shakeEffect(this.btn_box);
        this.addGif();
        EffectUtil.instance().scaleEffect(this.group_new);
        EffectUtil.instance().rockEffect(this.img_guide);
        EffectUtil.instance().rotationEffect(this.img_circle, 1000);
	}

    /**
     * 声音
     */
    private soundHandle()
    {
        if(TTSoundManager.instance().isBGMPlay)
        {
            this.btn_sound.currentState = 'disabled';
        }
        else
        {
            this.btn_sound.currentState = 'enabled';
        }
        TTSoundManager.instance().swBGM();
        TTSoundManager.instance().swAudio();
    }

    /**
     * 世界排行
     */
    private rankHandle()
    {
        let avatar_url = RoleCache.instance().avatar_url;
        if(avatar_url != '')
        {
            let data = {
                isPop:true,
                effect:2
            }
            UIManager.instance().openUILogic(WorldRankUI, data);
        }
        else
        {
            //授权
            let data = {
                isPop:true,
                effect:1,
                showBG:true,
                type:confirmType.openWorldRank
            }
            UIManager.instance().openUILogic(WechatConfirmUI, data);
        }
    }

    /**
     * 开始游戏
     */
    protected startGame()
    {
        // RES.getResAsync('fruit_json',()=>{
            ScreenMovies.MovieStart(2,()=>{
                // let curCustoms = GameManager.instance.curCustoms;
                // if(curCustoms % DataJson.const_define.task_customs.award == 0)
                // {
                //     //任务关卡
                //     let da = {
                //         index:0
                //     }
                //     UIManager.instance().openUILogic(TaskCustomsUI);
                // }
                // else
                // {
                let da = {
                    index:0
                }
                UIManager.instance().openUILogic(GameUI,da);
                // }
            },this);
        // },this)
    }

    protected removeEvent()
    {
        // SoundManager.instance().stop();
        this.btn_customs.removeTapHandler(); 
        this.btn_sound.removeTapHandler();
        // this.btn_rank.removeTapHandler();
        this.btn_sign.removeTapHandler();
        this.btn_gift.removeTapHandler();
        this.btn_getGold.removeTapHandler();
        this.btn_all.removeTapHandler();
        this.btn_friendRank.removeTapHandler();
        this.btn_worldRank.removeTapHandler();
        this.group_new.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.newWeponHandle, this);
        this.img_guide.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.showGuide, this);
        this.btn_award.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.AwardHadnle,this);
        GameUtil.removeEventListener(EventConst.UPDATE_REDPOINT, this.updateRedPointHandler, this);
        this.Timer.removeEventListener(egret.TimerEvent.TIMER, this.timerHandle, this);
        // this.group_touch.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.hideBox, this);
        // this.more_group.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.moreGroupShow, this);
    }

    public clear()
    {
        super.clear();
        this.Timer.stop();
        this.Timer = null;

        // EffectUtil.instance().StopEffect(this.btn_box);
        EffectUtil.instance().StopEffect(this.group_new);
        EffectUtil.instance().StopEffect(this.img_guide);
        EffectUtil.instance().StopEffect(this.img_circle);

        this.system.stop();
        this.system.parent.removeChild(this.system);
        this.system = null;

        // this.aim_box.remove();
        // this.aim_box = null;

        this.resourceItem.remove();
        this.resourceItem = null;

        if(this.activity_star && this.activity_star.parent)
        {
            this.activity_star.stop();
            this.activity_star.parent.removeChild(this.activity_star);
            this.activity_star = null;
        }
    }
}